#Requires AutoHotkey v2.0
#SingleInstance Force
#NoTrayIcon
/*
=======first_start=======
*/
msg := "
    (
    Intro：
        HarmonyOS NEXT系统手机截图存本地
    Tips：
        电脑安装HiSuite华为手机助手
        手机打开开发者模式
        手机数据线连电脑
        手机打开USB调试
    P.S.：
        本提示只会在第一次打开软件时显示
    )"
if IniRead('key_map_config.ini', 'keymap', 'first_start', '0') = '0' {
    MsgBox(msg)
    IniWrite('1', 'key_map_config.ini', 'keymap', 'first_start')
}
/*
=======init=======
*/
CoordMode('ToolTip', 'Screen')
CoordMode('Mouse', 'Screen')
; 初始化路径
hdc := A_WorkingDir . '\toolchains\hdc'
dir_path := IniRead('key_map_config.ini', 'keymap', 'dir_path', A_Desktop)
RunWait(hdc ' start', , 'Hide')
; 初始化快捷键
Hotkey("F1", capture, 'Off')
Hotkey("F3", capture, 'Off')
Hotkey("F6", capture, 'Off')
Hotkey("F7", capture, 'Off')
Hotkey("F8", capture, 'Off')
Hotkey("F9", capture, 'Off')
Hotkey("F10", capture, 'Off')
Hotkey(IniRead('key_map_config.ini', 'keymap', 'key', 'F1'), capture, 'On')
/*
=======function=======
*/
select_save_folder(*) {
    global dir_path
    dir_path := DirSelect(, 3)
    if dir_path = '' {
        Exit()
    }
    IniWrite(dir_path, 'key_map_config.ini', 'keymap', 'dir_path')
    G_bar.SetText(dir_path)
}
capture(*) {
    MouseGetPos(&px, &py)
    ToolTip('Shoting...', px, py)
    time := A_Now
    local_snap_path := dir_path . '\' . time . '.png'
    RunWait(hdc . ' shell uitest screenCap -p /data/local/tmp/' . time . '.png', , 'Hide') ; 截图
    RunWait(hdc . ' file recv /data/local/tmp/' . time . '.png ' . local_snap_path, , 'Hide') ; 截图存本地
    if FileExist(local_snap_path) {
        ToolTip('Done')
    } else {
        ToolTip('Faild')
    }
    Sleep(1000)
    ToolTip()
}
set_hotkey(*) {

    Hotkey("F1", , 'Off')
    Hotkey("F3", , 'Off')
    Hotkey("F6", , 'Off')
    Hotkey("F7", , 'Off')
    Hotkey("F8", , 'Off')
    Hotkey("F9", , 'Off')
    Hotkey("F10", , 'Off')

    Hotkey(G_key.Text, capture, 'On')
    IniWrite(G_key.Text, 'key_map_config.ini', 'keymap', 'key')
}
/*
=======GUI=======
*/
G := Gui('+MaxSize400x100 +MinSize400x100 +Resize +MinimizeBox -MaximizeBox')
G.MarginX := 10, G.MarginY := 10
G.SetFont('S12', 'Microsoft YaHei UI')
G_key := G.AddDDL('W150', ["F1", "F3", "F6", "F7", "F8", "F9", "F10"])
G_key.Text := IniRead('key_map_config.ini', 'keymap', 'key', 'F1')
G_key.OnEvent('Change', set_hotkey)
G.AddButton('W150 YM', 'Select Save Folder').OnEvent('Click', select_save_folder)
G.AddLink('YM', '<a href="https://gitee.com/tandaishi/next_shot">Gitee</a>')
G_bar := G.AddStatusBar('', dir_path)
G.Show()
OnExit((*) => RunWait(hdc . ' kill'))